<?php
function formatsize($fileSize) {
	$size = sprintf("%u", $fileSize);
	if($size == 0) {
		return("0 Bytes");
	}
	$sizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
	return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizename[$i];
}

/**
*	通用获取商品描述方法。
*	$cateId		类别ID
**/
function getGoodsTitle($goodsId,$default_value=''){
		if(empty($goodsId))return $default_value;
		$goods = M('Goods')->where('id='.$goodsId)->find();
		if(empty($goods))return $default_value;
		return $goods['title'];
}

/**
*	通用获取商户名称方法。
*	$cateId		商户ID
**/
function getGoodsEnable($goodsId,$default_value='E'){
		if(empty($goodsId))return $default_value;
		$goods = M('Goods')->where('id='.$goodsId)->find();
		if(empty($goods))return $default_value;
		if($goods['is_delete']=='Y')return 'D';
		return $goods['enable'];
}


/**
*	通用获取商户名称方法。
*	$cateId		商户ID
**/
function getParnterEnable($partId,$default_value='E'){
		if(empty($partId))return $default_value;
		$partner = D('Partner','Admin')->where('id='.$partId)->find();
		if(empty($partner))return $default_value;
		if($partner['is_delete']=='Y')return 'D';
		return $partner['enable'];
}


/**
*	通用获取商户名称方法。
*	$cateId		商户ID
**/
function getParnterTitleByGoodsId($goodsId,$default_value=''){
		if(empty($goodsId))return $default_value;
		$goods = D('Goods','Admin')->where('id='.$goodsId)->find();
		if(empty($goods))return $default_value;
		return getParnterTitle($goods['partner_id'],$default_value);
}

/**
*	通用上架商品ID获取商户名称方法。
*	$shelfId		上架ID
**/
function getProductInfoByShelfId($shelfId){
		if(empty($shelfId))return $default_value;
		$product = service('Product')->getByShelfId($shelfId);
		//if(empty($goods))return $default_value;
		return $product;
}

/**
*	通用上架商品ID获取商品名称方法。
*	$shelfId		上架ID
**/
function getGoodsTitleByShelfId($shelfId,$default_value=''){
		if(empty($goodsId))return $default_value;
		$goods = D('Goods','Admin')->where('id='.$goodsId)->find();
		if(empty($goods))return $default_value;
		return getParnterTitle($goods['partner_id'],$default_value);
}

/**
*	通用获取商户名称方法。
*	$cateId		商户ID
**/
function getParnterIdByGoodsId($goodsId){
		if(empty($goodsId))return '';
		$goods = D('Goods','Admin')->where('id='.$goodsId)->find();
		if(empty($goods))return '';
		return $goods['partner_id'];
}

/**
*	通用获取商户商标
*	$pid		商户ID
**/
function getParnterIconById($pid,$default_value=''){
		if(empty($pid))return $default_value;
		$partner = D('Partner','Admin')->where('id='.$pid)->find();
		return $partner['brand_icon'];
}

/**
*	查询商户对应商品数量
**/
function getGoodsNoByPartnerId($partnerId){
		if(empty($partnerId))return 0;
		$count = D('Goods','Admin')->where('partner_id='.$partnerId." and is_delete='N'")->count();
		if(empty($count))return 0;
		return $count;
}


//获取用户姓名
function getAdminName($uid,$lang='zh'){
	static $_MapName = array();
	if(!isset($_MapName[$uid])){
		if(is_numeric($uid)){
			$map['uid']   = $uid;
		}else{
			$map['uname'] = $uid;
		}
		$userinfo = M('Admin')->where($map)->field('uname')->find();
		$_MapName[$uid] = $userinfo['uname'];
	}
	return htmlspecialchars($_MapName[$uid]);
}


/*
 *获取用户组。（暂时未用到）
 */
function getAdminGroupIcon($uid){
		return '';
	//return Model('UserGroup')->getUserGroupIcon($uid);
}

/**
 * 将数组转换成php代码
 * @param unknown_type $ary_content
 */
function _createPHPCode($ary_content){
	$result = 'array(';
	foreach($ary_content as $key=>$val){
		if(is_array($val)){
			$result = $result."\n\t'{$key}'=>".$this->_createPHPCode($val)."";
		}else{
			$result = $result."\n\t'{$key}'=>'{$val}', ";
		}
	}
	$result = $result."\n\t),";
	return $result;
}

function write2ConfigFile($filename,$ary_newval,$ifclear=false){
	$filepath = K_CONFIG_PATH.$filename;//'config_hp.php';
	if($ifclear){
		$oldcontent = (include $filepath);
		//$content1[time()]=array('test'=>time(),'test1'=>time());
		//$content1['www']=time();
	}
	$content = "<?php\nif (!defined('SITE_PATH')) exit();\n\t";
	$content = $content."return ";
	$content = $content.$this->_createPHPCode($oldcontent);
	$content = substr($content,0,strlen($content)-1);
	$content = $content.";\n?>";
	file_put_contents($filepath,$content);
}
?>